package com.x.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * ClassName: MyInterceptor
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.09.14 1:32
 */
public class MyInterceptor implements HandlerInterceptor {

    /**
     * 执行handler之前调用的拦截方法（相当于Filter的doFilter()方法）
     * @param request 请求
     * @param response 响应
     * @param handler 要调用的方法对象
     * @return true为放行，false为拦截
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("Do preHandle()");
        return true;
    }



    /**
     * 只有preHandle()方法return true才会触发
     * handler执行完毕后，不需要做拦截了
     * 对结果处理，如敏感词汇检查
     * @param request
     * @param response
     * @param handler
     * @param modelAndView 返回的视图和共享域数据
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("Do postHandle()");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }


    /**
     * 整体处理完毕后调用可能该方法
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("Do afterCompletion()");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
